package com.shexianyu.medium;

/**
 * @author shexianyu
 * @desc 372. 超级次方
 * @date 2021/12/5
 */
public class SuperPow {
    public int superPow(int a, int[] b) {
        if (a == 1) return 1;

        int mod = 1337;
        int pow = 0;
        for (int i = 0; i < b.length; i++) {
            pow += b[i] * Math.pow(10, (b.length - i - 1));
        }
        a = (int) Math.pow(a, pow);
        return a % mod;
    }

    /**
     * 官方题解
     */

    public static final int MOD = 1337;

    public int superPow1(int a, int[] b) {
        int ans = 1;
        for (int i = b.length - 1; i >= 0; --i) {
            ans = (int) ((long) ans * pow(a, b[i]) % MOD);
            a = pow(a, 10);
        }
        return ans;
    }

    public int pow(int x, int n) {
        int res = 1;
        while (n != 0) {
            if (n % 2 != 0) {
                res = (int) ((long) res * x % MOD);
            }
            x = (int) ((long) x * x % MOD);
            n /= 2;
        }
        return res;
    }

}
